/*
 * Created on Jun 24, 2005
 */
package org.vectrics.recruiter.job.event;

import java.util.Locale;
import java.util.Set;

import org.apache.log4j.Logger;
import org.vectrics.common.util.DateSync;
import org.vectrics.event.Event;
import org.vectrics.event.EventEmailBuilder;
import org.vectrics.hibernate.ThreadProperties;
import org.vectrics.project.task.Task;
import org.vectrics.project.task.TaskCategory;
import org.vectrics.project.task.TaskStatusType;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.recruiter.job.JobStatus;
import org.vectrics.recruiter.job.JobStatusReasonType;
import org.vectrics.recruiter.job.JobStatusType;
import org.vectrics.user.PersonSearchCriteria;

/**
 * @author MMoore
 */
public class JobStatusChangeEventHandler extends JobEventHandler {
	private transient final Logger log = Logger.getLogger(this.getClass()); 
	
	/**
	 * 
	 */
	public JobStatusChangeEventHandler() {
		super();
	}
	
	protected EventEmailBuilder getEmailBuilder(Event event, Locale locale) {
		return(new JobStatusChangeEmailBuilder(event, this.getJob(), locale));
	}
	
	protected void doHandleEvent(Event event) {
		log.debug("doHandleEvent(event) - process status change");
		if (this.getJob().getCurrentStatus().getStatusType().getCode().equals(JobStatusType.STATUS_POSTED)) {
			
			log.debug("Assign task to primary recruiter to find candidate list");
			TaskCategory taskCategory = CoreServiceLocator.getProjectService().findTaskCategory("job.find.candidates");
			Task task = new Task();
			task.setCompany(this.getJob().getCompany());
			task.setDescription("task.description.job.create_cand_list");
			task.setDueDate(DateSync.getCurrentTime());
			task.setTaskCategory(taskCategory);
			task.addResource(this.getJob().getPrimaryRecruiter(), "Primary recruiter");
			task.setStatus(TaskStatusType.TASK_STATUS_ASSIGNED_CODE, ThreadProperties.getPerson());
			CoreServiceLocator.getProjectService().updateTask(task);
		}
		
		JobStatus currentStatus = this.getJob().getCurrentStatus();
		if (currentStatus.getStatusType().equals(JobStatusType.POST_PENDING)) {
			if (false == currentStatus.getStatusReasonType().getCode().equals(JobStatusReasonType.POST_PENDING_WAIT_FOR_DATE)) {
				TaskCategory taskCategory = CoreServiceLocator.getProjectService().findTaskCategory("job.post");
				Task task = new Task();
				task.setCompany(this.getJob().getCompany());
				task.setDescription("task.description.job.post");
				task.setDueDate(DateSync.getCurrentTime());
				task.setTaskCategory(taskCategory);
				task.addResource(this.getJob().getPrimaryRecruiter(), "Primary recruiter");
				task.setStatus(TaskStatusType.TASK_STATUS_ASSIGNED_CODE, ThreadProperties.getPerson());
				CoreServiceLocator.getProjectService().updateTask(task);
			}
		}
		
	}
	
	protected Set findNotifyPersons() {
		Set notifyPersons = super.findNotifyPersons();
		
		PersonSearchCriteria criteria = CoreServiceLocator.createPersonSearchCriteria();
		criteria.addDomain(this.getJob().getDomain());
		criteria.addPrivilege("job.status.change.notify");
		notifyPersons.addAll(CoreServiceLocator.getPersonService().findPersons(criteria, 0, 1000));
		
		return(notifyPersons);
	}
}
